/**
 * 
 */
package com.res.common.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.res.simulator.handler.SubmitOrderHandler;
import com.res.simulator.model.SubmitOrderModel;
import com.res.simulator.view.SubmitOrderView;

/**
 * @author Dhanushka Ariyasingha
 * 
 * 
 */
@Controller
@SessionAttributes
public class SubmitOrderController extends BaseController {

	@RequestMapping("/submitorderhome")
	public String home(
			@ModelAttribute("SubmitOrderModel") SubmitOrderModel submitOrderModel) {

		final Map<String, String> storeFrontMap = new LinkedHashMap<String, String>();

		storeFrontMap.put("CSSC", "CSSC");
		storeFrontMap.put("SHOP", "ATG");
		storeFrontMap.put("PLUM", "PLUM");
		storeFrontMap.put("BusinessDelegate", "DELEGATE");
		storeFrontMap.put("MyVerizon2", "MyVerizon2");

		getSession().setAttribute("channelMap", storeFrontMap);
		
		return "submitorderlayout";
	}
	
	@RequestMapping(value = "/purchaseOrder", method = RequestMethod.GET, params = "PurchaseOrder")
	protected String purchaseOrder(@ModelAttribute("SubmitOrderModel") SubmitOrderModel submitOrderModel,
			final BindingResult result) throws Exception {
		
		final String valueString = (String) getSession().getAttribute("HiddenValueString");
		if (null != valueString) {
			submitOrderModel.setHiddenValueString(valueString);
		}
		
		final SubmitOrderHandler handler = new SubmitOrderHandler();
		final SubmitOrderView view = handler.process(submitOrderModel);

		List<SubmitOrderView> viewCollection = (List<SubmitOrderView>) getSession().getAttribute("SubmitOrderViewCollection");

		if (null == viewCollection) {
			viewCollection = new ArrayList<SubmitOrderView>();
		}

		viewCollection.add(view);

		getSession().setAttribute("SubmitOrderViewCollection", viewCollection);
		getSession().setAttribute("HiddenValueString", submitOrderModel.getHiddenValueString());
		
		return "redirect:submitorderhome.htm";
	}


	@RequestMapping(value = "/purchaseOrder", method = RequestMethod.GET, params = "ClearOrderData")
	protected String clearOrder(
			@ModelAttribute("SubmitOrderModel") SubmitOrderModel submitOrderModel,
			final BindingResult result) {

		if (null != submitOrderModel) {
			submitOrderModel.clear();
		}

		List<SubmitOrderView> viewCollection = (List<SubmitOrderView>) getSession().getAttribute("SubmitOrderViewCollection");

		if (null != viewCollection) {
			viewCollection.clear();
			getSession().setAttribute("SubmitOrderViewCollection", viewCollection);
		}

		return "redirect:submitorderhome.htm";
	}

	@RequestMapping(value = "/purchaseOrder", method = RequestMethod.GET, params = "ResetInput")
	protected String reset(@ModelAttribute("SubmitOrderModel") SubmitOrderModel submitOrderModel,
			final BindingResult result) throws Exception {
		
		if (null != submitOrderModel) {
			submitOrderModel.clear();
		}
		
		return "redirect:submitorderhome.htm";
	}
		
}
